


//
//  ZSubGradeViewController.m
//  ZQLearn
//
//  Created by macfeel on 2019/5/10.
//  Copyright © 2019 macfeel. All rights reserved.
//
#import "ZBookMsgDetailVC.h"
#import "ZSubGradeViewController.h"
#import "SubjectCell.h"
#import "HomeModel.h"

@interface ZSubGradeViewController ()
<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,assign)NSInteger currentIndex;
@property (nonatomic,assign)NSInteger currentPage;

@end

@implementation ZSubGradeViewController
#define cellIdentifier @"subject"

-(UITableView *)tableView{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 1.5, ScreenWidth , ScreenHeight-ScreenHeightFlex-52) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
                _tableView.rowHeight = 100;
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"SubjectCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
       
        WeakStament(ws);
        _tableView.mj_header = [CustomRefreshHeader headerWithRefreshingBlock:^{
            [ws refreshData];
        }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = [NSMutableArray array];
    [self.view addSubview:self.tableView];
    self.currentIndex = 1;
    self.currentPage = 1;
    // Do any additional setup after loading the view.
}



#pragma mark - datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
//    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    HomeSubModel *model = self.dataArray[indexPath.row];
     SubjectCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.nameLab.text = model.name;
    cell.msgLab.text = model.bookInfo[@"content"];
    [cell.headImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kPicServerUrl,model.bookInfo[@"photo"]]] placeholderImage:nil];
    return cell;

}

#pragma delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ///跳转到书籍详情
    HomeSubModel *hmodel = self.dataArray[indexPath.row];
    ZBookMsgDetailVC *det = [ZBookMsgDetailVC new];
    MidModel *model = [MidModel new];
    model.name = hmodel.name;
    model.ID = hmodel.bookInfo[@"id"];
    model.catelogType = hmodel.bookInfo[@"catelogType"];
    YYLog(@"%@",model.catelogType);
    model.picUrl = [NSString stringWithFormat:@"%@%@",kPicServerUrl,hmodel.bookInfo[@"photo"]];
    det.MModel = model;
    
    [self.navigationController pushViewController:det animated:YES];
    
}


#pragma mark ZJScrollPageViewChildVcDelegate
- (void)zj_viewWillAppearForIndex:(NSInteger)index{
    
    YYLog(@"%ld",index);
    [UserModel shareInstance].tabIndex = index;
    self.currentIndex = index;
    
}
- (void)zj_viewDidAppearForIndex:(NSInteger)index{
    
}
- (void)zj_viewWillDisappearForIndex:(NSInteger)index{
    
}
- (void)zj_viewDidDisappearForIndex:(NSInteger)index{
    
}
- (void)zj_viewDidLoadForIndex:(NSInteger)index{
    //这里具体处理请求逻辑不同的页面根据索引进行请求
    YYLog(@"%ld",index);
    self.currentIndex = index;
    //        [self loadDataWithIndex:index];
    ///这个就直接根据科目id请求对应数据了，和索引暂时没关系了
    [self loadDataWithId:self.subId];
    
}
- (void)loadDataWithId:(NSString *)Id{
    
    if (self.currentPage==1) {
        [self.dataArray removeAllObjects];
    }
    NSDictionary *dic = @{@"gradeSubjectInfo.id":self.subId?:@"",@"pageSize":@(50),@"pageIndex":@(1)};
    WeakStament(ws);
    [ZNetworkService postRequestWithBaseUrl:kGradeAccordBooksUrl setParameters:dic success:^(id result) {
        
        YYLog(@"%@",result);
        if ([result[@"errorCode"]integerValue]==0) {
            NSArray *arr = result[@"result"];
            for (NSDictionary *dic in arr) {
                HomeSubModel *model = [HomeSubModel mj_objectWithKeyValues:dic];
                YYLog(@"%@%@%@",model.name,model.bookInfo[@"content"],model.bookInfo[@"photo"]);
                [ws.dataArray addObject:model];
            }
            [ws.tableView reloadData];
            [ws endRefresh];
        }else{
            [AFToast showText:result[@"errorMsg"]];
            [ws endRefresh];
        }
        
    } fail:^(NSError *error) {
        
        YYLog(@"%@",error);
        [self endRefresh];
        
    } delegater:nil];
}

- (void)refreshData{
    
    self.currentPage = 1;
    [self loadDataWithId:self.subId];
}

- (void)endRefresh{
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

- (void)loadMoreData{
    
}


@end
